home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / CDaemon / HACK16 / Sources / ATA Utility.c next >
Encoding:
Text File  |  2001-06-23  |  15.4 KB  |  611 lines

  1. /*
  2.     File:        ATA Utlity.c - Based on Apple's ATA Demo.c
  3.     
  4.     Description:ATADemo is a CodeWarrior C sample that will call the ATA
  5.                  Manager and scan the ATA bus and list the various device
  6.                  configuration data.  
  7.      
  8.                 This sample has been updated to properly work with the .AppleCD
  9.                 driver installed.
  10.  
  11. */
  12.  
  13. //    This has been modfied a bit by me -Shawn Platkus
  14.  
  15. #ifdef __cplusplus
  16.     extern "C" {
  17. #endif
  18.  
  19. //------------------------------------------------------------------------------------
  20. #pragma mark Includes
  21. //------------------------------------------------------------------------------------
  22. #include <TextUtils.h>
  23. #include <Patches.h>
  24. #include <Traps.h>
  25. #include <Devices.h>
  26. #include <ATA.h>
  27.  
  28. #include "MyIncludes.h"
  29.  
  30. //------------------------------------------------------------------------------------
  31. #pragma mark Globals
  32. //------------------------------------------------------------------------------------
  33.  
  34. UInt8    gATAMgrPBVers;    // used to hold current version of PB from ataMgrInquiry
  35.  
  36.  
  37. FormatTable kIdentFormat[] = {
  38.     {     0,     0, kHex,        "Configuration word"                        },
  39.     {     1,     1,    kDecimal,    "Cylinders"                                    },
  40.     {     3,     3,    kDecimal,    "Heads"                                        },
  41.     {     4,     4,    kDecimal,    "Bytes/Track"                                },
  42.     {     5,     5,    kDecimal,    "Bytes/Sector (0 = 512 bytes)"                },
  43.     {     5,     5,    kDecimal,    "Sectors/Track"                                },    /* 35 */
  44.     {     7,     9,    kHex,        "Vendor Unique (word 7..9)"                    },
  45.     {    10,    19,    kRightJust,    "Serial Number"                                },
  46.     {    20,    20,    kHex,        "Buffer Type"                                },
  47.     {    21,    21,    kDecimal,    "Buffer size in 512 byte increments"        },
  48.     {    22,    22,    kDecimal,    "Number of ECC bytes available"                },
  49.     {    23,    26,    kLeftJust,    "Firmware Revision"                            },
  50.     {    27,    46,    kLeftJust,    "Model Number"                                },
  51.     {    47,    53,    kHex,        "Capability Flags (word 47..53)"            },
  52.     {    54,    54,    kDecimal,    "Cylinders (current mode)"                    },
  53.     {    55,    55,    kDecimal,    "Heads (current mode)"                        },
  54.     {    56,    56,    kDecimal,    "Sectors/Track (current mode)"                },
  55.     {    57,    58,    kDecimal,    "Current Capacity in Sectors"                },
  56.     {    59,    59,    kHex,        "Multiple Sector Capability Flag"            },
  57.     {    60,    61,    kDecimal,    "Current User-addressable Sectors"            },
  58.     {    62,    62,    kHex,        "Single Word DMA Transfer Mode Flags"        },
  59.     {    63,    63,    kHex,        "Multiword DMT Transfer Mode Flags"            },
  60.     {     0,  0,    kEndOfTable,""                                            }
  61. };
  62.  
  63. //------------------------------------------------------------------------------------
  64. #pragma mark -
  65. //------------------------------------------------------------------------------------
  66.  
  67.  
  68. // ---------------------------------------------------------------------------
  69. OSErr         DisplayATAManagerInquiryInfo (void)
  70. // ---------------------------------------------------------------------------
  71. //
  72. //  Display information about the ATA Manager
  73. //
  74. {
  75.     ataMgrInquiry    pb;
  76.     OSErr            status;
  77.  
  78.     CLEAR(pb);
  79.     
  80.     pb.ataPBFunctionCode =    kATAMgrManagerInquiry;
  81.     pb.ataPBVers =            kATAPBVers1;
  82.  
  83.     status = ataManager((ataPB*) &pb );
  84.  
  85.     IF_ERROR(status, "ATA Manager Inquiry failed\n")
  86.  
  87.     printf("ATA Manager inquiry:\n");
  88.     PrintNumVersion("\tATA Manager Version:", pb.ataMgrVersion);
  89.     printf("\tBusses:\t\t%d\n\tDevices:\t%d\n",
  90.                 pb.ataBusCnt, pb.ataDevCnt);
  91.     printf("\tPIO Modes:\t%x\n\tDMA Modes:\t%d\n\tMulti DMA Modes:%x\n",
  92.                  pb.ataPioModes, pb.ataSingleDMAModes, pb.ataMultiDMAModes);
  93.     printf("-----------------------------------------\n\n");    
  94.     
  95.     gATAMgrPBVers = pb.ataMgrPBVers;    // remember current PB version
  96.     
  97.     return (status);
  98. }
  99.  
  100.  
  101. // ---------------------------------------------------------------------------
  102. OSErr         ScanATABusses (void)
  103. // ---------------------------------------------------------------------------
  104. //
  105. //  Display information about the ATA Busses and returns the ID of a CD-ROM drive
  106. //  or -1
  107. //
  108. {
  109.     ataDrvrRegister        pb;
  110.     OSErr                status;
  111.     StringPtr            drvrNamePtr;
  112.     UInt32                nDeviceCD;
  113.  
  114. // Get first device ID (yes you have to do this once)
  115.     CLEAR(pb);
  116.     pb.ataPBFunctionCode     =    kATAMgrFindDriverRefnum;
  117.     pb.ataPBVers            =    kATAPBVers1;
  118.     pb.ataPBDeviceID         =     (UInt32)0x0000ffff;
  119.     status                     =     ataManager((ataPB*) &pb );
  120.  
  121. // loop through devices to find CD drive - store its ID in nDeviceCD
  122.     nDeviceCD = -1;
  123.     for    (pb.ataPBDeviceID = (UInt32) pb.ataDeviceNextID;
  124.          pb.ataPBDeviceID != 0xff;
  125.          pb.ataPBDeviceID = (UInt32) pb.ataDeviceNextID)
  126.         {             
  127.             status = ataManager((ataPB*) &pb );
  128.             IF_ERROR(status, "ATA Find Driver failed\n")
  129.             
  130. #ifdef DEVELOPMENT    // for debugging
  131.             printf("\nDevice %d %#s\n", pb.ataPBDeviceID, DrvrRefToName(pb.ataDrvrRefNum) );
  132. #endif
  133.             (char *)drvrNamePtr = DrvrRefToName(pb.ataDrvrRefNum);
  134.  
  135.             if ( EqualString(drvrNamePtr, "\p.AppleCD", false, true) )
  136.             {
  137. #ifdef DEVELOPMENT    // for debugging
  138.                 printf("(This is a CD-ROM drive.) \n");
  139. #endif
  140.                 if (nDeviceCD == -1)
  141.                     nDeviceCD = pb.ataPBDeviceID;
  142.             }            
  143.             
  144. #ifdef DEVELOPMENT    // for debugging
  145.         printf("-----------------------------------------\n\n");    
  146. #endif
  147.         }
  148.  
  149.     status = nDeviceCD;
  150.     return (status);
  151. }
  152.  
  153.  
  154.  
  155. // ---------------------------------------------------------------------------
  156. OSErr         DisplayATADriveIdentity (UInt32 deviceID)
  157. // ---------------------------------------------------------------------------
  158. //
  159. //  Display information about the ATA Identify Info
  160. //
  161. {
  162.     ataIdentify            pb;
  163.     ataDevConfiguration    pb1;
  164.     UInt8        devType;
  165.     UInt8        buffer[512];
  166.     OSErr        status;
  167.     UInt8        dataBuffer[4096];
  168.     
  169.     
  170.  
  171. // Get Driver Configuration
  172.     CLEAR(pb1);
  173.     pb1.ataPBFunctionCode     =    kATAMgrGetDrvConfiguration;
  174.     pb1.ataPBVers            =    kATAPBVers2;
  175.     pb1.ataPBDeviceID         =    deviceID;
  176.     
  177.     status = ataManager((ataPB*) &pb1 );
  178.     IF_ERROR(status, "ATA GetDrvConfiguration failed\n")
  179.  
  180.  
  181. // Setup Identify block;
  182.     CLEAR(pb);
  183.     pb.ataPBFunctionCode     =    kATAMgrDriveIdentify;
  184.     pb.ataPBVers            =    kATAPBVers1;
  185.     pb.ataPBDeviceID        =    deviceID;
  186.      pb.ataPBFlags             =     mATAFlagIORead + mATAFlagByteSwap ;
  187.      
  188.      if(pb1.ataDeviceType == kDevATAPI) pb.ataPBFlags += mATAFlagProtocol1;
  189.      
  190.     pb.ataPBTimeOut            =    100;
  191.     pb.ataPBBuffer            =    buffer;
  192. //    pb.ataPBBuffer            =    (void*) buffer;
  193.     
  194.     status = ataManager((ataPB*) &pb );
  195.     IF_ERROR(status, "ATA DriveIdentify failed\n")
  196.  
  197.     printf("Configuration: (");
  198.  
  199.     switch(pb1.ataDeviceType){
  200.         case kDevUnknown:    
  201.                         printf("Unknown protocol");
  202.                         devType = 0;
  203.                         break;
  204.         
  205.         case kDevATA:
  206.                         printf("ATA");
  207.                         devType = kDevATA; 
  208.                         break;
  209.                              
  210.         case kDevATAPI:    
  211.                         printf("ATAPI"); 
  212.                         devType = kDevATAPI; 
  213.                         break;
  214.                         
  215.         case kDevPCMCIA:    
  216.                         printf("PCMCIA"); 
  217.                         devType = kDevATA; 
  218.                         break;
  219.     }
  220.     
  221.     switch(pb1.ataSocketType){
  222.         case kSocketUnknown:    
  223.                         printf(", Unknown Socket"); 
  224.                         break;
  225.         
  226.         case kSocketInternal:
  227.                         printf(", Internal"); 
  228.                         break;
  229.                              
  230.         case kSocketMediaBay:    
  231.                         printf(", Media Bay"); 
  232.                         break;
  233.                         
  234.         case kkSocketPCMCIA:    
  235.                         printf(", PCMCIA, Vcc=%d, Vpp1=%d, Vpp2=%d", 
  236.                                         pb1.atapcVcc, pb1.atapcVpp1,pb1.atapcVpp2); 
  237.                         break;
  238.     }
  239.  
  240.     printf(")\n");
  241.  
  242.     DumpFormatedBuffer(buffer, kIdentFormat);
  243.     
  244.     // read and display sector 0
  245.     if( (devType == kDevATA) )
  246.     {
  247.         status = ATARead( pb1.ataPBDeviceID, dataBuffer );
  248.         
  249.         if( status == noErr )
  250.         {
  251.             printf( "\n*********************************** Sector 0 ***********************************\n\n" );
  252.             DumpRawBuffer( dataBuffer, 512 );
  253.         }
  254.         else
  255.             printf( "\nATA read failed: %d\n", status );
  256.     }
  257.     else if( devType == kDevATAPI )
  258.     {
  259.         status = ATAPIRead( pb1.ataPBDeviceID, dataBuffer );
  260.         
  261.         if( status == noErr )    
  262.         {
  263.             printf( "\n*********************************** Sector 0 ***********************************\n\n" );
  264.             DumpRawBuffer( dataBuffer, 2048 );
  265.         }
  266.         else
  267.             printf( "\nATAPI read failed: %d\n", status );
  268.     }
  269.         
  270.     return status;
  271. }
  272.  
  273.  
  274.  
  275. // ---------------------------------------------------------------------------
  276. OSErr         DisableCDDriver (SInt16 refNum)
  277. // ---------------------------------------------------------------------------
  278. //
  279. // Disable the CD driver
  280. //
  281. {    OSErr status;
  282. #ifdef DEVELOPMENT    // for debugging
  283.     printf("- Disable the CD Driver \n");
  284. #endif
  285.  
  286.      // wake up drive
  287.     status = WakeUpCDDrive (refNum);
  288.     if (status != noErr)
  289.     {
  290.         return status;
  291.     }
  292.  
  293.     // move driver to quiescent mode
  294.     status = SetQuiescence (refNum, quiescenceON);
  295.     if (status != noErr)
  296.     {
  297.         return status;
  298.     }
  299.  
  300.     return noErr;
  301. }
  302.  
  303. // ---------------------------------------------------------------------------
  304. OSErr         EnableCDDriver (SInt16 refNum)
  305. // ---------------------------------------------------------------------------
  306. //
  307. //  Re-enable the CD driver
  308. //
  309. {    OSErr status;
  310.  
  311. #ifdef DEVELOPMENT    // for debugging
  312.     printf("- Enable the CD Driver \n");
  313. #endif
  314.  
  315.     // move driver to quiescent mode
  316.     status = SetQuiescence (refNum, quiescenceOFF);
  317.     if (status != noErr)
  318.     {
  319.         return status;
  320.     }
  321.  
  322.     return noErr;
  323. }
  324.  
  325. // ---------------------------------------------------------------------------
  326. OSErr         WakeUpCDDrive (SInt16 refNum)
  327. // ---------------------------------------------------------------------------
  328. //
  329. //  Re-enable the CD driver
  330. //
  331. {    CntrlParam    pb;
  332.     OSErr        status;
  333.  
  334. #ifdef DEVELOPMENT    // for debugging
  335.     printf("-- Wake up the CD Driver \n");
  336. #endif
  337.  
  338.     CLEAR(pb);
  339.  
  340.      pb.ioCRefNum = refNum;
  341.     pb.csCode = csSetPowerMode;
  342.     *(UInt8 *) &pb.csParam[0] = pmActive;
  343.     
  344.   // send command to the CD/DVD driver
  345.     status = PBControlImmed ((ParmBlkPtr) &pb);
  346.  
  347.      return status;
  348. }
  349.  
  350. // ---------------------------------------------------------------------------
  351. OSErr         SetQuiescence (SInt16 refNum, UInt16 mode)
  352. // ---------------------------------------------------------------------------
  353. //
  354. //  Re-enable the CD driver
  355. //
  356. {
  357.     CntrlParam    pb;
  358.     OSErr        status;
  359.  
  360. #ifdef DEVELOPMENT    // for debugging
  361.     printf("-- Set the CD Driver Quiescence mode to %s\n",  
  362.         (mode ==quiescenceOFF)? "Off":"On");
  363. #endif
  364.  
  365.     CLEAR(pb);
  366.  
  367.      pb.ioCRefNum = refNum;
  368.     pb.csCode = csQuiescence;
  369.     pb.csParam[0] = mode;
  370.     
  371.   // send command to the CD/DVD driver
  372.     status = PBControlImmed ((ParmBlkPtr) &pb);
  373.  
  374.      return status;
  375. }
  376.  
  377.  
  378. // ---------------------------------------------------------------------------
  379. Boolean     ATAManagerPresent    (void)
  380. // ---------------------------------------------------------------------------
  381. //
  382. // returns true if this machine has the ata manager
  383. //
  384. {
  385.         return (TrapAvailable(kATATrap));
  386. }
  387.  
  388. // ---------------------------------------------------------------------------
  389. Boolean     ATAHardwarePresent        (void)
  390. // ---------------------------------------------------------------------------
  391. //
  392. // returns true if this machine has ata hardware
  393. //
  394. {
  395.     UInt16    configFlags;
  396.  
  397.     // Hardware configuration flags
  398.     configFlags = LMGetHWCfgFlags();
  399.     
  400.     return (configFlags & 0x0080);
  401. }
  402.  
  403. //------------------------------------------------------------------------------------
  404. #pragma mark -
  405.  
  406. #define NumToolboxTraps() (                                \
  407.         (NGetTrapAddress(_InitGraf, ToolTrap)            \
  408.                 == NGetTrapAddress(0xAA6E, ToolTrap))    \
  409.             ? 0x200 : 0x400                                \
  410.     )
  411. #define GetTrapType(theTrap) (                            \
  412.         (((theTrap) & 0x800) != 0) ? ToolTrap : OSTrap    \
  413.     )
  414.  
  415. // ---------------------------------------------------------------------------
  416. Boolean     TrapAvailable        (short theTrap)
  417. // ---------------------------------------------------------------------------
  418. // (see Inside Mac VI 3-8)
  419. {
  420.         TrapType                trapType;
  421.         
  422.         trapType = GetTrapType(theTrap);
  423.         if (trapType == ToolTrap) {
  424.             theTrap &= 0x07FF;
  425.             if (theTrap >= NumToolboxTraps())
  426.                 theTrap = _Unimplemented;
  427.         }
  428.         return (
  429.             NGetTrapAddress(theTrap, trapType)
  430.             != NGetTrapAddress(_Unimplemented, ToolTrap)
  431.         );
  432. }
  433.  
  434. // ---------------------------------------------------------------------------
  435. void         PrintNumVersion        (char *label, NumVersion version )
  436. // ---------------------------------------------------------------------------
  437. //
  438. //    Decode version number info
  439. //
  440. {
  441.     char            *stage;
  442.  
  443.     switch (version.stage) {
  444.     case developStage:    stage = "d";        break;
  445.     case alphaStage:    stage = "a";        break;
  446.     case betaStage:        stage = "b";        break;
  447.     case finalStage:    stage = "";            break;
  448.     default:            stage = "?";        break;
  449.     
  450.     }
  451.     printf("%s %d.%d.%d",
  452.         label,
  453.         version.majorRev,
  454.         (version.minorAndBugRev>>4), (version.minorAndBugRev & 0xf),
  455.         stage);
  456.     if(version.stage != finalStage) printf(".%d",version.nonRelRev);
  457.     
  458.     printf(" (hex %08lx)\n",    (* ((unsigned long *) &version)));
  459. }
  460.  
  461.  
  462. // ---------------------------------------------------------------------------
  463. void        DumpFormatedBuffer    (void* p,  const FormatTablePtr format) 
  464. // ---------------------------------------------------------------------------
  465. //
  466. // Dump formatted buffer
  467. //
  468.  
  469. {    
  470.     unsigned short            *idInfo = (unsigned short *)p;
  471.  
  472.     register char            *charField;
  473.     register short            fieldLength;
  474.     register short            i;
  475.     register long            value;
  476.     register FormatTablePtr    formatPtr;
  477.     
  478. #define FORMAT    (*formatPtr)
  479.  
  480.     for (formatPtr = format; FORMAT.format != kEndOfTable; formatPtr++) {
  481.             printf("%35s", FORMAT.label);
  482.             switch (FORMAT.format) {
  483.             // Decimal values come as two words with the words swapped.
  484.             case kDecimal:
  485.                 value = 0;
  486.                 for (i = FORMAT.lastWord; i >= FORMAT.firstWord; i--) {
  487.                     value <<= 16;
  488.                     value += idInfo[i];
  489.                 }
  490.                 printf(" %lu", value);    // was %lu
  491.                 break;
  492.                 
  493.             case kHex:
  494.                 for (i = FORMAT.lastWord; i >= FORMAT.firstWord; i--) {
  495.                     value = idInfo[i];
  496.                     printf(" 0x%04X", value);
  497.                 }
  498.                 break;
  499.                 
  500.             case kLeftJust:
  501.                 charField = (char *) &idInfo[FORMAT.firstWord];
  502.                 fieldLength = (FORMAT.lastWord - FORMAT.firstWord + 1)
  503.                             * sizeof (unsigned short);
  504.                 /*
  505.                  * First scan for an unspecified field.
  506.                  */
  507.                 for (i = 0; i <= fieldLength; i++) {
  508.                     if (charField[i] != '\0')
  509.                         goto gotLeftJustField;
  510.                 }
  511.                 printf(" <not specified>");
  512.                 break;
  513.                 
  514. gotLeftJustField:
  515.                 for (i = fieldLength; i > 0; --i) {
  516.                     if (charField[i - 1] != ' ')
  517.                         break;
  518.                 }
  519.                 printf(" \"%.*s\"", (int) i, charField);
  520.                 break;
  521.                 
  522.             case kRightJust:
  523.                 charField = (char *) &idInfo[FORMAT.firstWord];
  524.                 fieldLength = (FORMAT.lastWord - FORMAT.firstWord + 1)
  525.                             * sizeof (unsigned short);
  526.                 /*
  527.                  * First scan for an unspecified field.
  528.                  */
  529.                 for (i = 0; i <= fieldLength; i++) {
  530.                     if (charField[i] != '\0')
  531.                         goto gotRightJustField;
  532.                 }
  533.                 printf(" <not specified>");
  534.                 break;
  535.                 
  536. gotRightJustField:
  537.                 for (i = 0; i < fieldLength; i++) {
  538.                     if (charField[i] != ' ')
  539.                         break;
  540.                 }
  541.                 printf(" \"%.*s\"", (int) (fieldLength - i), &charField[i]);
  542.                 break;
  543.                 
  544.             }
  545.             printf("\n");
  546.         }
  547. //        DumpRawBuffer( p,512);
  548. }    
  549.     
  550. // ---------------------------------------------------------------------------
  551. void        DumpRawBuffer        ( UInt8 *bufferPtr, int length )
  552. // ---------------------------------------------------------------------------
  553. //
  554. // Dump buffer
  555. //
  556.  
  557. {
  558.         register int            i;
  559.         int                        lineStart;
  560.         int                        lineLength;
  561.         short                    c;
  562.  
  563. #define kLineSize    16
  564.         for (lineStart = 0; lineStart < length; lineStart += lineLength) {
  565.             lineLength = kLineSize;
  566.             if (lineStart + lineLength > length)
  567.                 lineLength = length - lineStart;
  568.             printf("%03x %3d:", lineStart, lineStart);
  569.             for (i = 0; i < lineLength; i++)
  570.                 printf(" %02x", bufferPtr[lineStart + i] & 0xFF);
  571.             for (; i < kLineSize; i++)
  572.                 printf("   ");
  573.             printf("  ");
  574.             for (i = 0; i < lineLength; i++) {
  575.                 c = bufferPtr[lineStart + i] & 0xFF;
  576.                 if (c > ' ' && c < '~')
  577.                     printf("%c", c);
  578.                 else {
  579.                     printf(".");
  580.                 }
  581.             }
  582.             printf("\n");
  583.         }
  584. }
  585.  
  586.  
  587.         
  588. // ---------------------------------------------------------------------------
  589. char*        DrvrRefToName(short refNum)
  590. // ---------------------------------------------------------------------------
  591. //
  592. //  lookup driver name in table
  593. //
  594.  
  595. {
  596.         AuxDCEHandle*        UTable  = (AuxDCEHandle*) LMGetUTableBase();
  597.         DCtlPtr                dctl;
  598.         Ptr                    p;    
  599.         
  600.         if(!refNum) return ((char*) "\p<none>");
  601.  
  602.         dctl = (DCtlPtr) *UTable[~refNum];
  603.         p      =  dctl->dCtlDriver;
  604.         if( dctl->dCtlFlags  & 0x0040) p = (char *) *p;
  605.  
  606.         return  ( p?(char*) (p+18):(char*)"\p-Purged-");
  607. }
  608.  
  609. #ifdef __cplusplus
  610.     }
  611. #endif